home *** CD-ROM | disk | FTP | other *** search
- /***
- *setup.c - disk editor setup
- *
- *Copyright (c) 1991-1994, Gregg Jennings. All wrongs reserved.
- * P O Box 200, Falmouth, MA 02541-0200
- *
- *Purpose:
- * Initialization.
- *
- *Notice:
- * This progam may be freely used and distributed. Any distrubution
- * with modifications must retain the above copyright statement and
- * modifications noted.
- * No pulp-publication, in whole or in part, permitted without
- * permission (magazines or books).
- *******************************************************************************/
-
- /*
- Versions:
-
- 2.1 04-Jan-1994 added get_volume()
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <string.h>
- #include <ctype.h>
- #include <malloc.h>
- #include <fcntl.h>
- #include <io.h>
- #include <direct.h>
- #include <errno.h>
-
- #include "disked.h"
- #include "diskio.h"
- #include "files.h"
- #include "mylib.h"
- #include "alloc.h"
- #include "error.h"
- #include "direct.h"
-
- const char *Author="Gregg Jennings";
- const char *Title="DISKED The DISK EDitor";
- const char *Copyr="(c) 1989-1994, All wrongs reserved";
- const char *Namep="DISKED";
-
- #ifdef _QC
- const char *Version=" 2.5.A.1293";
- #elif __BORLANDC__
- const char *Version=" 2.5.B.1293";
- #else
- const char *Version=" 2.5.C.1293";
- #endif
-
- char *disked_ini="DISKED.INI";
- char *disked_sav="DISKED.SAV";
-
- extern char curdir[67]; /* current directory each drive */
- extern char comspec[FILENAME_MAX]; /* for shell command */
- extern char inifile[67]; /* initialization file name */
- extern char savfile[67]; /* save place file name */
- extern int Save,Restore; /* more switches */
- extern int files_indexed; /* files-indexed flag */
- extern int find_flag; /* find string arg */
- #ifdef SET_TO_DIR
- extern int dir_flag; /* set-to dir arg */
- extern char dir_str[67];
- #endif
- extern char tmpfstr[]; /* find string, entry */
- extern char findstr[]; /* find string, actual */
- extern unsigned int findlen; /* find string, length */
- extern int min_mem; /* minimize menory allocation */
- extern int write_to; /* double protection on write */
-
- extern unsigned long tagged[10]; /* "tagged" sectors */
-
- extern unsigned int byte_cnt; /* buffer byte count */
- extern unsigned int max_bytes; /* maximum buffer size */
- extern int Signon; /* signon message */
- extern unsigned char *save_sec; /* duplicate sector buffer */
- extern unsigned char *spare_sec; /* spare sector buffer */
-
- void _cdecl _far hhandler(unsigned deverr, unsigned doserr, unsigned _far *hdr);
-
- /*
- * SETUP new drive, valid drive array, drive to use
- *
- * ver 2.4 03-Dec-1993 added /@
- * ver 2.3 20-Nov-1993 order of options parsed
- * ver 2.2 13-Nov-1993 better finding of INI file, newalloc()
- * ver 2.1 19-Apr-1992 Added /?
- */
-
- int setup(char **argv, int drives[], unsigned int *dsk)
- {
- int c;
- unsigned i,tempd,cdisk;
- char tmpstr[MAXSTR+1];
- char prompt_env[FILENAME_MAX];
- char *tempc;
-
- write_to = FALSE;
- byte_cnt = 0;
- files_indexed = FALSE;
- Signon = TRUE;
- Save = Restore = FALSE;
- find_flag = min_mem = 0;
- n_files = 1;
- n_dirs = 1;
-
- /* get drive info */
-
- max_drive=0;
- _dos_getdrive(&cdisk); /* get current drive */
- *dsk=cdisk; /* copy it */
- for (i=0;i<MAX_DRIVE;i++) /* loop to verify drive # */
- {
- _dos_setdrive(i,&tempd); /* try and set */
- _dos_getdrive(&tempd); /* see if it was set */
- if (i==tempd)
- {
- drives[i]=TRUE;
- ++max_drive;
- }
- else
- drives[i]=FALSE;
- }
-
- _dos_setdrive(cdisk,&tempd); /* reset current disk */
-
- strcpy(comspec,getenv("COMSPEC")); /* get where COMMAND.COM is */
- strcpy(prompt_env,getenv("PROMPT")); /* change prompt for shell */
- strcpy(tmpstr,"PROMPT=");
- strcat(tmpstr,prompt_env);
- strcat(tmpstr,"(DE)");
- putenv(tmpstr);
-
- tempd = 1; /* for INI file test */
-
- if ((tempc=getenv("INIT"))!=NULL)
- {
- strcpy(savfile,tempc); /* get env dir */
- strcpy(inifile,tempc);
- if (inifile[strlen(inifile)-1]!='\\')
- {
- strcat(savfile,"\\");
- strcat(inifile,"\\");
- }
- strcat(savfile,disked_sav);
- strcat(inifile,disked_ini);
- tempd = !exist(inifile);
- }
- if (tempd) /* if INI file not in dir */
- {
- strcpy(savfile,*argv); /* get where DISKED resides */
- strcpy(inifile,*argv);
- i=strlen(inifile);
- inifile[--i]='I';
- inifile[--i]='N';
- inifile[--i]='I';
- i=strlen(savfile);
- savfile[--i]='V';
- savfile[--i]='A';
- savfile[--i]='S';
- }
-
- /* initialize */
-
- init(inifile);
-
- /* capture INT 24 handler */
-
- _harderr(hhandler);
-
- /* check command line parameters */
-
- if (*++argv)
- {
- do
- {
- c=tolower(**argv);
- if (c == '-') /* turn things off */
- {
- switch (*++*argv)
- {
- case 's': Save=FALSE; break;
- case 'r': Restore=FALSE; break;
- case 'f': Files=FALSE; break;
- case 'h': Home=FALSE; break;
- default: continue; break;
- }
- }
- else if (c == '/') /* do/set things */
- {
- switch (*++*argv)
- {
- case 'h': /* help ! */
- case '?':
- command_line();
- exit(0);
- break;
- case 'x': /* set INI file name */
- ++*argv;
- if (*argv)
- strcpy(inifile,*argv);
- break;
- case 's': /* set SAV file name */
- ++*argv;
- if (*argv)
- strcpy(savfile,*argv);
- break;
- case 'm': /* minimize memory */
- min_mem = 1;
- break;
- case 'f': /* find string arg */
- ++*argv;
- if (*argv[0]) /* be careful of space after 'f' */
- {
- find_flag = 1;
- strcpy(findstr,*argv);
- }
- else
- {
- if (*++argv)
- {
- find_flag = 1;
- strcpy(findstr,*argv);
- }
- else
- *--argv; /* fool the do while loop */
- }
- break;
- case '@':
- Signon = 0;
- break;
- default:
- continue;
- break;
- }
- }
- else if (c == '+') /* turn things on */
- {
- switch (*++*argv)
- {
- case 's': Save=TRUE; break;
- case 'r': Restore=TRUE; break;
- case 'f':
- if (find_flag == 1)
- find_flag = 2; /* index files AFTER find() */
- Files = TRUE;
- break;
- case 'h': Home=TRUE; break;
- default: continue; break;
- }
- }
- else if (isalpha(c)) /* disk drive letter */
- {
- tempd=toupper(**argv)-'@'; /* make valid */
- if (drives[tempd]) /* check it */
- {
- *dsk=tempd; /* log it */
- Restore=FALSE; /* drive spec higher precedence */
- /*Save=FALSE;*/ /* than Restore */
- }
- }
- } while (*++argv);
- }
- return(cdisk);
- }
-
- /* define NON-DOS formats */
- /*#define DISKTYPE /* not implemented yet */
-
- void command_line(void)
- {
- print("\n\t");print(Copyr);print(Version);
- #ifdef DISKTYPE
- print("\n\tDISKED [-|+srfh] [/x{inifile}] [/s{savefile}] [/f{text}] [/m] [d:] [/td:nnn]\n\n");
- #else
- print("\n\tDISKED [-|+srfh] [/x{inifile}] [/s{savefile}] [/f{text}] [/m] [d:]\n\n");
- #endif
- print("\toptions: - off + on\n");
- print("\t s save\n");
- print("\t r restore\n");
- print("\t f files\n");
- print("\t h home\n\n");
- print("\t {inifile} initialization file override\n");
- print("\t {savefile} save file override\n");
- print("\t {text} find text\n");
- print("\t m minimize file buffer size\n");
- print("\t d: drive\n");
- #ifdef DISKTYPE
- print("\t t:nnn disk type:\n");
- #endif
- }
-
- void signon_msg(void)
- {
- send('\n');
- printc(Title);
- printc(Copyr);
- printc("This is Freeware.");
- printc(__DATE__);
- print("\n\t This program is still being improved and feedback from");
- print("\n\tusers is extremely essential!\n");
- print("\n\t Anyone sending me their name and address is entitled for");
- print("\n\ta postscript printed manual, free future versions and support.\n");
- print("\n\t NOTE: It is okay to give this program away.\n");
- print("\n\t ALSO: Please read the documentation that should come with");
- print("\n\tthis program for important copyleft information. If the");
- print("\n\tdocumentation is missing, please write me for a copy.\n");
- print("\n\t AND: This version LETS YOU WRITE to the disk. NO WARRANTY");
- print("\n\tis made, expressed, or implied.\n");
- print("\n\t THANK YOU Gregg Jennings");
- print("\n\t P O Box 200");
- print("\n\t Falmouth MA 02540-0200\n");
- }
-
- /*
- Read DISKED.INI and set the world.
-
- ver 2.0 removed "boot" and "video" 02-Dec-1993
- ver 1.1 added "Expert"
- */
-
- void init(char *filename)
- {
- char string[100], *seps = " \t\n,:=";
- register char *p;
- FILE *file;
-
- if (stricmp(filename,"none")==0 || filename[0]=='\0')
- return;
-
- if ((file=fopen(filename,"r"))==NULL)
- {
- if (errno==ENOENT) {
- errno=0;
- file=fopen(disked_ini,"r");
- }
- if (errno!=0)
- {
- if (errno!=ENOENT)
- perror(filename);
- return;
- }
- }
-
- while (!feof(file))
- {
- if (fgets(string,100,file) == NULL)
- break;
-
- if ((p = strtok(string,seps)) == NULL)
- continue;
-
- if (*p == ';' || *p == '/')
- continue;
-
- if (stricmp("radix",p) == 0)
- {
- p = strtok(NULL,seps);
- if (p)
- if ((Radix = numarg(p,Radix)) < 1 || Radix >26)
- Radix = 10;
- continue;
- }
- else if (stricmp("ascii",p) == 0)
- {
- p = strtok(NULL,seps);
- if (p) Ascii = boolarg(p);
- continue;
- }
- else if (stricmp("convert",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Convert = boolarg(p);
- continue;
- }
- else if (stricmp("display",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Display = boolarg(p);
- continue;
- }
- else if (stricmp("files",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Files = boolarg(p);
- continue;
- }
- else if (stricmp("get",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Get = boolarg(p);
- continue;
- }
- else if (stricmp("home",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Home = boolarg(p);
- continue;
- }
- else if (stricmp("verify",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Verify = boolarg(p);
- continue;
- }
- else if (stricmp("partial",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Partial = boolarg(p);
- continue;
- }
- else if (stricmp("mask",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Mask = boolarg(p);
- continue;
- }
- else if (stricmp("strip",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Strip = boolarg(p);
- continue;
- }
- else if (stricmp("write",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) write_to = boolarg(p);
- continue;
- }
- else if (strcmp("Expert",p)==0)
- {
- write_to = Write = Files = Translate = 1;
- Display = Verify = Home = Strip = Signon = 0;
- continue;
- }
- else if (stricmp("signon",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Signon = boolarg(p);
- continue;
- }
- else if (stricmp("debug",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Debug = boolarg(p);
- continue;
- }
- else if (stricmp("save",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Save = boolarg(p);
- continue;
- }
- else if (stricmp("restore",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Restore = boolarg(p);
- continue;
- }
- else if (stricmp("translate",p)==0)
- {
- p = strtok(NULL,seps);
- if (p) Translate = boolarg(p);
- continue;
- }
- }
- }
-
- /* save position, tagged sectors, find string */
-
- /* to do: save all switches */
-
- void saveinit(char *filename)
- {
- FILE *file;
- int i;
-
- if ((file=fopen(filename,"w"))==NULL)
- {
- perror(filename);
- return;
- }
- fputs("; DISKED.SAV\n",file);
- fprintf(file,"disk %c:\n",disk+'A'-1);
- fprintf(file,"sector %u\n",log_sector);
- fputs("tagged ",file);
- for (i=1;i<10;i++)
- fprintf(file,"%ld ",tagged[i]);
- fputs("\t;tagged sectors\n",file);
- if (tmpfstr[0])
- fprintf(file,"find %s\n",tmpfstr);
- fclose(file);
- }
-
- /* restore position, tagged sectors, find string */
-
- /* to do: restore all switches */
-
- int getinit(char *filename)
- {
- char string[100], *seps = " \t\n,:=";
- register char *p;
- FILE *file;
-
- if ((file=fopen(filename,"r"))==NULL)
- {
- if (errno!=ENOENT)
- perror(filename);
- return(0);
- }
-
- while (!feof(file))
- {
- if (fgets(string,100,file) == NULL)
- break;
-
- if (((p = strtok(string,seps)) == NULL) || (*p == ';' || *p == '/'))
- continue;
-
- if (stricmp("disk",p) == 0)
- {
- p = strtok(NULL,seps);
- if (p) disk = keyarg(strupr(p),disk)-'A'+1;
- continue;
- }
- if (stricmp("sector",p) == 0)
- {
- p = strtok(NULL,seps);
- if (p) log_sector = (unsigned int)longarg(p,10);
- continue;
- }
- if (stricmp("tagged",p) == 0)
- { int i;
- for (i=0;i<10;i++)
- {
- p = strtok(NULL,seps);
- if (p) tagged[i] = numarg(p,10);
- }
- continue;
- }
- if (stricmp("find",p) == 0)
- {
- p = strtok(NULL,seps);
- if (p)
- {
- strcpy(tmpfstr,p);
- strcpy(findstr,p);
- findlen=convert(findstr); /* convert if needed */
- }
- continue;
- }
- }
- fclose(file);
- return(1);
- }
-
- unsigned int numarg(char *arg, int radix)
- {
- unsigned int i=0;
-
- if (radix==10)
- sscanf(arg,"%u",&i);
- else
- sscanf(arg,"%x",&i);
- return(i);
- }
-
- unsigned long longarg(char *arg,int radix)
- {
- unsigned long l=0L;
-
- if (radix==10)
- sscanf(arg,"%lu",&l);
- else
- sscanf(arg,"%lx",&l);
- return(l);
- }
-
- /*
- sets a boolean keyword: "on" returns 1
- "off" or NULL returns 0
- */
- int boolarg(char *arg)
- {
- return (arg && (stricmp("on",arg)==0 || stricmp("yes",arg)==0));
- }
-
- /* assigns a key value: "^a" - "^z" returns to 01 - 1Ah
- "" returns key
- "off" returns -1
- */
-
- int keyarg(char *arg,int key)
- {
- register int i;
-
- if (arg==NULL)
- i=key;
- else if (stricmp("off",arg)==0)
- i=-1;
- else if ((stricmp("^`",arg)+stricmp("^{",arg))==0)
- i=*(++arg)-'`';
- else
- i=*arg;
- return(i);
- }
-
- /* view file to screen */
-
- void viewfile(char *filename)
- {
- char buf[255];
- FILE *fh;
- int i=1;
-
- if (stricmp(filename,"none")==0 || filename[0]=='\0')
- return;
-
- if ((fh=fopen(filename,"r"))==NULL)
- {
- perror(filename);
- return;
- }
- if (Display)
- {
- conout(' ');
- print(filename);
- print(" File Contents:\n\n");
- }
- else
- send('\n');
- while (!feof(fh))
- {
- if (fgets(buf,255,fh)==NULL)
- break;
- printf(buf);
- if (++i==23)
- {
- print("<more>\r");
- i=input();
- if (i==0)
- input();
- print(" \r");
- if (i==ESC)
- break;
- else
- i=0;
- }
- }
- fclose(fh);
- }
-
- /* command prompt */
-
- int prompt(int n, int base)
- {
- unsigned int key;
-
- conout('(');
- pnlz(n,(base==16)?4:5,base);
- conout(')');
- if (byte_cnt==max_bytes)
- conouta(' ',15);
- if (Files && n_files>1)
- conout(15);
- else
- conout('*');
- bdos(0x0c,0,0);
- key=conin();
- if ((key&0xff)==0 || key>0x3920 || key<0x011B)
- return(key);
- return(key&0xff);
- }
-
- int do_files(void);
-
- /* consolated disk change stuff */
-
- int newdisk(int tempd)
- {
- int i;
- unsigned int temps;
-
- temps=sec_size; /* save current sector size */
- savesector(); /* and sector data */
- if ((i=logdisk(tempd)) != DISK_OK)
- {
- restoresector();
- return(0);
- }
- if (temps!=sec_size) /* new sector size */
- {
- sec_buf = newalloc(sec_buf,sec_size*sizeof(char));
- if (sec_buf == NULL)
- {
- print("\nReallocation Failure.\n");
- print("Sorry, but I am too confused to continue...\n");
- exit(3);
- }
- save_sec = newalloc(save_sec,sec_size*sizeof(char));
- spare_sec = newalloc(spare_sec,sec_size*sizeof(char));
- }
- getcwd(curdir,66); /* get current directory */
- n_files = 1; /* setup for files */
- n_dirs = 1;
-
- if (Files) /* if to index them */
- {
- if (files_indexed) /* if already */
- {
- freep(clusters); /* free them first */
- freep(files);
- }
- do_files();
- }
- if (!Files)
- get_volume(volume);
-
- memset(tagged,0,sizeof(tagged));
-
- if (Home)
- rootsector();
- else
- {
- /*
- temps = (unsigned)cwdstart();
- setsector(temps);
- */
- if (log_sector>=num_sectors)
- lastsector();
- else
- readsector();
- }
- return(1);
- }
-
- char *harderr_list[] = {
- "Disk is write-protected",
- "Unknown unit",
- "Drive not ready",
- "Unknown command",
- "CRC error in data",
- "Bad drive-request structure length",
- "Seek error",
- "Unknown media type",
- "Sector not found",
- "Printer out of paper",
- "Write fault",
- "Read fault",
- "General failure",
- };
-
- /* Handler to deal with hard error codes. Since DOS is not reentrant,
- * it is not safe to use DOS calls for doing I/O within the DOS Critical
- * Error Handler (int 24h) used by _harderr. Therefore, screen output and
- * keyboard input must be done through the BIOS.
- */
- void _cdecl _far hhandler(unsigned deverr, unsigned doserr, unsigned _far *hdr)
- {
- if (doserr >= 0 && doserr <= 0x0C)
- error.msg = harderr_list[doserr];
- error.num = doserr;
- error.mod = error.func = "harderr";
- _hardretn(doserr&0xff);
- }
-
- /* exit() shell */
-
- void quit(int e)
- {
- print(err_msg[e]);
- exit(e);
- }
-
- /* common index files stuff */
-
- int do_files(void)
- {
- if (Display)
- bprint(" indexing...");
- files_indexed = Files = initfiles();
- if (Display)
- clreol();
- return files_indexed;
- }
-